<?php if(isset($success_message) == 1){  ?>
        <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
            <div class="noty_message"><span class="noty_text">Usuario creado con éxito!</span></div>
        </div>
<?php }elseif(isset($success_message) == 0){ ?>
<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error hidden" id="message_error_formAddPurchase" style="cursor: pointer; left: 559.5px">
    <div class="noty_message"><span id="contentMessageErrorFormAddPurchase" class="noty_text">Complete los datos del formulario</span></span></div>
</div>
<?php } ?>

<?php if(isset($edit) && $edit == 1) { ?>
       <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
            <div class="noty_message"><span class="noty_text">Usuario editado con éxito!</span></div>
        </div> 
<?php } ?>
<?php if(isset($success_message_3)) { ?>
       <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
            <div class="noty_message"><span class="noty_text">Contraseña Editada con éxito!</span></div>
        </div> 
<?php } ?>

<div class="grid_container">
           <script>
    function soloLetras(e){
       key = e.keyCode || e.which;
       tecla = String.fromCharCode(key).toLowerCase();
       letras = " áéíóúabcdefghijklmnñopqrstuvwxyz";
       especiales = [8,37,39,46];

       tecla_especial = false
       for(var i in especiales){
            if(key == especiales[i]){
                tecla_especial = true;
                break;
            }
        }

        if(letras.indexOf(tecla)==-1 && !tecla_especial){
            return false;
        }
    }
</script>
   <script type="text/javascript">
   $(document).ready(function(){
           $('#boton_guardar_user').click(function(){
                  
                 var alias = $.trim($('input[name=alias]').val());
                 var name = $.trim($('input[name=name]').val());
                 var pass = $.trim($('input[name=pass]').val());
                 var rol = $.trim($('select[name=rol]').val());
                 
                        if(alias != '' && name != '' && pass != '' && rol != 0){
                                
                                
                                
                                <?php if(isset($user->id)){  ?>
                                   $('#formAddUsers').submit();
                                 <?php }else{ ?>
                                 $.ajax({
                                        'url': url+'/users/detected_alias',
                                        'type': 'POST',
                                        'data': { 'alias': $('input[name=alias]').val() },
                                        'success': function(res) {
                                                if(res >= 1){
                                                        $('input[name=alias]').css('background','#ff0000');     
                                                        alert('El Alias ya fue ingresado anteriormente en el sistema!');
                                                        $('#noty_success_1355937644582').fadeOut('fast');
                                                }else if(res <= 0){
                                                        $('input[name=alias]').css('background','#ffffff');     
                                                        $('#formAddUsers').submit();
                                                }
                                        }
                                  });
                                 <?php }?>                      
                        }else{
                                alert('Rellene los resgitros del formulario');
                                $('#noty_success_1355937644582').fadeOut('fast');
                        }
                         
           })  
           
            $('#cambio_pass').click(function(){
                 $.ajax({
                        'url': url+'/users/edit_pass',
                        'type': 'POST',
                        'data': {  'id' : $('input[name=user_id]').val() , 'pass': $('input[name=pass]').val() },
                        'success': function(res) {
                                $('#formAddUsers').submit();
                        }
                  });
           })  
           
        })
   </script>
    <form id="formAddUsers" class="form_container" action="<?php echo base_url(); ?>index.php/users/submit" method="post"> 
    <div class="grid_12">
        <div class="widget_wrap">
                <div class="widget_top">
                        <span class="h_icon user_2"></span>
                        <h6><?=isset($user->id) ? 'Editar' : 'Agregar' ?> Usuario</h6>
                </div>
            <div class="widget_content">
                <ul>
                        <li>
                            <div class="form_grid_12">
                                    <div class="form_input">
                                            <div class="form_grid_4 alpha">
                                                    <span class="field_title">Alias <span class="req">*</span></span>
                                                   
                                                    <input type="text" name="alias" id="alias_user" class="noEmpty" value="<?php echo @field(set_value('alias'), $user->alias); ?>" tabindex="1" />
                                            </div>
                                            <span class="clear"></span>
                                    </div>
                            </div>
                        </li>


                        <li>
                            <div class="form_grid_12">
                                    <div class="form_input">
                                            <div class="form_grid_4 alpha">
                                                    <span class="field_title">Nombre y Apellido <span class="req">*</span></span>

                                                    <input type="text" id="name" name ="name" onkeypress="return soloLetras(event)"  class="noEmpty" value="<?php echo @field(set_value('name'), $user->name); ?>"  tabindex="2"/>
                                            </div>
                                            <span class="clear"></span>
                                    </div>
                            </div>
                        </li>
                        <?php if (!isset($user->id)) : ?>
                        <li>
                            <div class="form_grid_12">
                                    <div class="form_input">
                                            <div class="form_grid_4 alpha">
                                                    <span class="field_title">Password <span class="req">*</span></span>
                                                   
                                                    <input type="text" id="pass" name ="pass" class="noEmpty" value="<?php echo @field(set_value('pass'), $user->password); ?>"  tabindex="3"/>
                                            </div>
                                            <span class="clear"></span>
                                    </div>
                            </div>
                        </li>
                        <?php endif; ?>
                        <li>
                            <div class="form_grid_12">
                                    <div class="form_input">
                                            <div class="form_grid_12 alpha">
                                                    <span class="field_title">Rol <span class="req">*</span></span>
                                                   <?php 
                                                    $options = array();
                                                    $options[0] = "";
                                                    foreach ($id_rol as $rl) {
                                                    $options[$rl->id_rol] = $rl->description;
                                                    }
                                        $extra = 'id="client" tabindex="4" style="width:340px" class="chzn-select full" data-placeholder="Elegir..."';
    						   echo form_dropdown('rol', $options, @field(set_value('id_rol'), $user->id_rol), $extra);
    								?>
                                            </div>
                                            <span class="clear"></span>
                                            </br>
                                            <button  id="boton_guardar_user" type="button" class="btn_small btn_orange"><span>Guardar</span></button>
                                            <button type="button" class="btn_small btn_orange"><span><a id="buscar_guia" href="<?php echo base_url(); ?>index.php/users/">Cancelar</a></span></button>
                                    </div>
                            </div>
                            
                        </li>
                </ul>
                <span class="clear"></span>
            </div>
        </div>
        <br />
                <?php echo form_hidden("user_id", @field($user->id, "")); ?>
                
    </div>
    <span class="clear"></span>
    <?php if (isset($user->id)) : ?>
        <div class="grid_12">
        <div class="widget_wrap">
                <div class="widget_top">
                        <span class="h_icon user_2"></span>
                        <h6>Cambiar Password</h6>
                </div>
            <div class="widget_content">
                <ul>
                    <li>
                             <div class="form_grid_12">
                                    <div class="form_input">
                                            <div class="form_grid_12 alpha">
                                                    <span class="field_title">Password <span class="req">*</span></span>
                                                    <input type="password" name="pass" id="pass" class="noEmpty" value="<?php echo @field(set_value('pass'), $user->password); ?>" tabindex="4"/>
                                            </div>
                                            <span class="clear"></span>
                                             </br>
                                             <button  id="cambio_pass" type="button" class="btn_small btn_orange"><span>Guardar</span></button>
                                    </div>
                            </div>
                        
                    </li>                        
                    </ul>
                        <span class="clear"></span>
                       
                        </div>
            </div>
            </div>                       
                <?php echo form_hidden("user_id", @field($user->id, "")); ?>
                
    </form>
    <span class="clear"></span>
   <?php endif; ?>
</div>